package com.hangoverstudios.batterysaver;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import android.app.Notification;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;

public class BatteryService extends Service {
	
	Helper helper;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	 @Override
    public void onCreate() 
 	{
      helper = new Helper(this);  
    }
	 
	 @Override
		public int onStartCommand(Intent intent, int flags, int startId)
	 	{
		 Intent batteryIntent = getApplicationContext().registerReceiver(null,
                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		 System.out.println("raj battery level onStartCommand");
		 int currentLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
         int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
         int level = -1;
         if (currentLevel >= 0 && scale > 0) {
             level = (currentLevel * 100) / scale;
         }		 
		 
         if((level <= 20  && level % 5 == 0) || level == 1)
         {	  
        	 System.out.println("raj battery level changed111111");
        	 if(!helper.isPowerSaveModeOn())
        	 {	        	
        		 System.out.println("raj battery level changed22222");
	        	 Intent batteryChecker = new Intent(this, BatteryLevelChecker.class);
	        	 batteryChecker.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        	 batteryChecker.putExtra("battery_level", level);
	        	 startActivity(batteryChecker);
        	 }
		 }
			
			return 0;			
	 	}
	 
	    @Override
	    public void onStart(Intent intent, int startId) 
	    {
	    	
	    }
}
